<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Admin as AdminModel;
use app\admin\validate\Admin as AdminValidate;
use Env;

class Admin extends Controller
{
    public function add(Request $request)
    {
        if($request->isPost()){
            //dump($request->param());die;
            $validate = new AdminValidate;
            //验证
            if(!$validate->scene('add')->check($request->param())){
                $this->error($validate->getError());
            }
            //添加
            $admin = new AdminModel;
            //allowField(true)过虑post数组中非数据表字段数据
            $admin->allowField(true)->save($request->param());
            $this->redirect('admin/Admin/index');
        }
        $roles = get_com_role();
        $this->assign('roles', $roles);
        return $this->fetch();
    }

    public function index($page = 1, $q = '')
    {
        $q = input('get.q');
        $pageParam = ['query' => []];

        if($q){
            $pageParam['query']['q'] = $q;
            $list = AdminModel::where('admin_name', 'LIKE', "%$q%")->order('admin_id desc')->paginate(15,false,$pageParam);
        }else{
            $list = AdminModel::where('admin_role', '>', 100)->order('admin_id desc')->paginate(15);
        }

        $adminCount = AdminModel::where('admin_role', '>', 100)->count();
        $this->assign('adminCount', $adminCount);
        $this->assign('list', $list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
    }

    public function edit($admin_id = 0, $page = 1, $q='')
    {
        $admin = AdminModel::get($admin_id);
        if($admin){
            $this->assign('admin', $admin);
            //用户权限
            $roles = get_com_role();
            $this->assign('roles', $roles);
        }else{
            echo '非法参数!';die;
        }

        if($this->request->isPost()){
            $validate = new AdminValidate;
            //验证
            if(!$validate->scene('edit')->check(input('post.'))){
                $this->error($validate->getError());
            }
            //更新
            $admin->tel = input('post.tel');
            $admin->admin_name = input('post.admin_name');
            $admin->unit = input('post.unit');
            $admin->admin_password = input('post.admin_password');
            $admin->admin_role = input('post.admin_role');
            $admin->save();
            //跳转
            return redirect('admin/Admin/index',['page' => $page, 'q' => $q]);
        }

        $this->assign('page', $page);
        return $this->fetch();
    }

    public function del($admin_id = 0)
    {
        $admin = AdminModel::get($admin_id);
        if($admin)
        {
            $this->assign('admin',$admin);
        } else {
            echo '非法参数！';die;
        }

        if($this->request->isPost()){
            AdminModel::destroy($admin_id);
            return redirect('admin/Admin/index');
        }
        return $this->fetch();
    }

    //导入用户数据
    public function import(Request $request)
    {
        if(request()->isPost())
        {
            // 获取表单上传文件
            $file = $request->file('excelTable');
            // 上传文件验证
            $result = $this->validate(['file' => $file], ['file'=>'require|fileExt:xls,xlsx'],['file.require' => '请选择上传文件', 'file.fileExt' => '非法excel文件']);
            if(true !== $result){
                $this->error($result);
            }
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(Env::get('root_path') . 'public/uploads/excel');
            if ($info) {
                //$this->success('文件上传成功：' . $info->getRealPath());die;
                //读取excel数据写入数据库
                $data = import_Excel($info->getRealPath(),$info->getExtension());
                //dump($data);die;
                foreach ($data as $key => $val) {
                    if ($key > 1) {
                        $datas['tel'] = $val['A'];
                        $datas['admin_password'] = '123456';
                        $datas['admin_name'] = $val['B'];
                        $datas['unit'] = $val['C'];
                        $datas['admin_role'] = $request->param('admin_role');

                        $newData[] = $datas;
                    }
                }
                $admin = new AdminModel;
                $result = $admin->saveAll($newData);
                //dump($result);die;
                return redirect('admin/Admin/index');
            } else {
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }

        $roles = get_com_role();
        $this->assign('roles', $roles);
        return $this->fetch();
    }
}
